import tkinter as tk
from tkinter import messagebox

def text_statistics_gui():
    # 创建主窗口
    root = tk.Tk()
    root.title("文本统计工具")
    root.geometry("600x400")
    root.resizable(False, False)

    # 初始化统计变量
    c_str_count = tk.IntVar(value=0)  # cfile的字符数
    w_word_count = tk.IntVar(value=0)  # wfile的单词数
    c_sen_count = tk.IntVar(value=0)  # cfile的句子数

    # 统计cfile.txt（字符数、句子数）
    def count_cfile():
        try:
            with open(r"D:\code\two\cfile.txt", "r", encoding="utf-8") as fp:
                t = fp.read(1)
                c_str = 0
                c_sen = 0
                while t:
                    # 统计字符数（所有字符）
                    c_str += 1
                    # 统计句子数（. ! ? 结尾）
                    if t in ('.', '!', '?'):
                        c_sen += 1
                    t = fp.read(1)
                c_str_count.set(c_str)
                c_sen_count.set(c_sen)
        except FileNotFoundError:
            messagebox.showerror("错误", "cfile.txt文件未找到，请检查路径：D:\\code\\two\\cfile.txt")
        except Exception as e:
            messagebox.showerror("错误", f"读取cfile.txt失败：{str(e)}")

    # 统计wfile.txt（单词数）
    def count_wfile():
        try:
            with open(r"D:\code\two\wfile.txt", "r", encoding="utf-8") as fp:
                t = fp.read(1)
                w_word = 0
                while t:
                    # 统计单词数（空格、逗号、标点分隔）
                    if t in (' ', ',', '.', '!', '?'):
                        w_word += 1
                    t = fp.read(1)
                w_word_count.set(w_word)
        except FileNotFoundError:
            messagebox.showerror("错误", "wfile.txt文件未找到，请检查路径：D:\\code\\two\\wfile.txt")
        except Exception as e:
            messagebox.showerror("错误", f"读取wfile.txt失败：{str(e)}")

    # 程序启动时自动统计
    def auto_count():
        count_cfile()
        count_wfile()

    # 创建界面组件
    # 标题
    tk.Label(root, text="文本统计结果", font=("微软雅黑", 14, "bold")).pack(pady=20)

    # 统计结果框架
    result_frame = tk.Frame(root, bd=2, relief=tk.GROOVE, padx=20, pady=10)
    result_frame.pack(fill=tk.X, padx=50)

    # cfile.txt 统计结果（字符数、句子数）
    tk.Label(result_frame, text="cfile.txt 统计：", font=("微软雅黑", 11, "bold")).grid(row=0, column=0, columnspan=2, pady=10, sticky=tk.W)
    
    tk.Label(result_frame, text="字符数：", font=("微软雅黑", 10)).grid(row=1, column=0, padx=30, pady=8, sticky=tk.W)
    tk.Label(result_frame, textvariable=c_str_count, font=("微软雅黑", 10), width=15, bg="#e0e0e0").grid(row=1, column=1, pady=8, sticky=tk.W)
    
    tk.Label(result_frame, text="句子数：", font=("微软雅黑", 10)).grid(row=2, column=0, padx=30, pady=8, sticky=tk.W)
    tk.Label(result_frame, textvariable=c_sen_count, font=("微软雅黑", 10), width=15, bg="#e0e0e0").grid(row=2, column=1, pady=8, sticky=tk.W)

    # wfile.txt 统计结果（单词数）
    tk.Label(result_frame, text="wfile.txt 统计：", font=("微软雅黑", 11, "bold")).grid(row=3, column=0, columnspan=2, pady=10, sticky=tk.W)
    
    tk.Label(result_frame, text="单词数：", font=("微软雅黑", 10)).grid(row=4, column=0, padx=30, pady=8, sticky=tk.W)
    tk.Label(result_frame, textvariable=w_word_count, font=("微软雅黑", 10), width=15, bg="#e0e0e0").grid(row=4, column=1, pady=8, sticky=tk.W)

    # 刷新和退出按钮
    btn_frame = tk.Frame(root, pady=20)
    btn_frame.pack()
    
    tk.Button(btn_frame, text="刷新统计", command=auto_count, width=12).grid(row=0, column=0, padx=20)
    tk.Button(btn_frame, text="退出", command=root.quit, width=12).grid(row=0, column=1, padx=20)

    # 启动时自动统计
    auto_count()

    root.mainloop()

if __name__ == "__main__":
    text_statistics_gui()